package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yxx.annotation.OptLogger;
import com.yxx.annotation.VisitLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.ArticleConditionQuery;
import com.yxx.entityModel.vo.query.CategoryQuery;
import com.yxx.entityModel.vo.request.CategoryRequest;
import com.yxx.entityModel.vo.response.ArticleConditionListResponse;
import com.yxx.entityModel.vo.response.CategoryBackResponse;
import com.yxx.entityModel.vo.response.CategoryOptionResponse;
import com.yxx.entityModel.vo.response.CategoryResponse;
import com.yxx.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler;

import javax.annotation.Resource;

import java.util.List;

import static com.yxx.constant.OptTypeConstant.*;

/**
 * 分类控制器
 * @author yuxiangxun
 * @date 2025/2/3
 * @apiNote
 */
@Api(tags = "分类模块")
@RestController
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 查看后台分类列表
     * @param categoryQuery
     * @return
     */
    @ApiOperation(value = "查看后台分类列表")
    @SaCheckPermission("blog:category:list")
    @GetMapping("/admin/category/list")
    public ResponseResult<PageResult<CategoryBackResponse>> CategoryBackResponseList(CategoryQuery categoryQuery){
        return ResponseResult.success(categoryService.categoryBackResponseList(categoryQuery));
    }

    /**
     * 添加分类
     * @param categoryRequest
     * @return
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加分类")
    @SaCheckPermission("blog:category:add")
    @PostMapping("/admin/category/add")
    public ResponseResult<?> addCategory(@RequestBody @Validated CategoryRequest categoryRequest){
        categoryService.addCategory(categoryRequest);
        return ResponseResult.success();
    }

    /**
     * 删除分类
     * @param categoryIdList 分类id集合
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除分类")
    @SaCheckPermission("blog:category:delete")
    @DeleteMapping("/admin/category/delete")
    public ResponseResult<?> deleteCategory(@RequestBody List<Integer> categoryIdList) {
        categoryService.deleteCategory(categoryIdList);
        return ResponseResult.success();
    }


    /**
     * 修改分类
     * @param category 分类信息
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改分类")
    @SaCheckPermission("blog:category:update")
    @PutMapping("/admin/category/update")
    public ResponseResult<?> updateCategory(@Validated @RequestBody CategoryRequest category) {
        categoryService.updateCategory(category);
        return ResponseResult.success();
    }

    /**
     * 查看分类选项
     * @return
     */
    @ApiOperation(value = "查看分类选项")
    @GetMapping("/admin/category/option")
    public ResponseResult<List<CategoryOptionResponse>> categoryOptionList(){
        return ResponseResult.success(categoryService.categoryOptionList());
    }

    /**
     * 查看分类列表
     * @return
     */
    @VisitLogger(value = "文章分类")
    @ApiOperation(value = "查看分类列表")
    @GetMapping("/category/list")
    public ResponseResult<List<CategoryResponse>> categoryList(){
        return ResponseResult.success(categoryService.categoryListVO());
    }

    /**
     * 查看分类下的文章
     * @param articleConditionQuery
     * @return
     */
    @VisitLogger(value = "分类文章")
    @ApiOperation(value = "查看分类下的文章")
    @GetMapping("/category/article")
    public ResponseResult<ArticleConditionListResponse> articleCategoryList(@Validated ArticleConditionQuery articleConditionQuery){
        return ResponseResult.success(categoryService.articleCategoryList(articleConditionQuery));
    }

}
